package com.iyoudoock.citymine;

import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iyoudoock.citymine.activity.MainActivity;
import com.iyoudoock.citymine.activity.MapActivity;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.iyoudoock.citymine.utils.SystemUtils;
import com.kvin.toolkit.utils.JumpUtils;
import com.kvin.toolkit.utils.system.AnimUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GuideActivity extends BaseActivity {

    @BindView(R.id.time_tv)
    TextView timeTv;
    @BindView(R.id.time_container)
    LinearLayout timeContainer;
    @BindView(R.id.version_tv)
    TextView versionTv;
    private Timer mTimer;

    private int limit = 3;//delay time
    private Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        hideActionbar();
    }

    @Override
    public void init() {
        timeContainer.setOnClickListener(this);

        mTimer = new Timer();
        animation = AnimationUtils.loadAnimation(this, R.anim.guide_animation_text);
        //handler.sendEmptyMessageDelayed(0, 1000);
        String version = SystemUtils.getVersionName(this);
        versionTv.setText(version);


        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(limit < 0 ? (limit = 0) : limit--);
            }
        }, 0, 1000);

        loadingData();//login
    }

    @Override
    public void loadingData() {
        phoneNo = mPersistentUtils.readString(Key.User.TELEPHONE);
        userPsw = mPersistentUtils.readString(Key.User.PASSWORD);
        if (TextUtils.isEmpty(mUid) && !TextUtils.isEmpty(phoneNo) && !TextUtils.isEmpty(userPsw)) {
            login(phoneNo, userPsw);
        }
    }

    @Override
    public void onHandleMessage(Message msg) {
        timeTv.setText(String.valueOf(msg.what));
        if (msg.what == 0) {
            mRoleType = mPersistentUtils.readString(Key.User.ROLE_TYPE);
            if (mRoleType.equals(Value.Role.USER) || TextUtils.isEmpty(mRoleType)) {
                JumpUtils.simpleJump(GuideActivity.this, MapActivity.class, null, true, AnimUtils.AnimStyle.RIGHT_IN_FADE_OUT);
                return;
            }
            JumpUtils.simpleJump(GuideActivity.this, MainActivity.class, null, true, AnimUtils.AnimStyle.RIGHT_IN_FADE_OUT);
        } else {
            animation.reset();
            timeTv.startAnimation(animation);
        }

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        JumpUtils.simpleJump(getActivity(), MapActivity.class, null, false);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
}
